#coding:utf-8

import json

# 均值,分布
def getCount(location1,location2):
    n = 0
    s = 0
    n1 = 0
    s1 = 0
    n0 = 0
    s0 = 0

    M1 = [{},{},{},{},{}]
    M0 = [{},{},{},{},{}]

    with open(location1) as f:
        for line in f:
            xs = json.loads(line)
            n += 1
            s += xs[-1]
            if int(xs[0]) == 1:
                n1 += 1
                s1 += xs[-1]
                ss = xs[-1]*10
                ss = int(ss)
                ss = ss/2
                M1[ss][xs[1]+"-"+xs[3]] = '%.4F'%xs[-1]+" "+xs[2]+" &&& "+xs[4]
            if int(xs[0]) == 0:
                n0 += 1
                s0 += xs[-1]
                ss = xs[-1]*10
                ss = int(ss)
                ss = ss/2
                M0[ss][xs[1]+"-"+xs[3]] = '%.4F'%xs[-1]+" "+xs[2]+" &&& "+xs[4]

    res =[n,s/n,n1,s1/n1,n0,s0/n0,s1/n1-s0/n0]
    res1 = [len(m) for m in M1]
    res0 = [len(m) for m in M0]
    print res
    print res1
    print res0
    print
    with open(location2,mode='w') as f:
        line = json.dumps(res,ensure_ascii=False)
        f.write(line.encode('utf-8')+'\n')
        line = json.dumps(res1,ensure_ascii=False)
        f.write(line.encode('utf-8')+'\n')
        #line = json.dumps(M1,ensure_ascii=False)
        #f.write(line.encode('utf-8')+'\n')
        for m in M1:
            for k,v in m.items():
                #line = json.dumps(m)
                line = json.dumps((k,v),ensure_ascii=False)
                f.write(line.encode('utf-8')+'\n')
            f.write("\n")
        line = json.dumps(res0,ensure_ascii=False)
        f.write(line.encode('utf-8')+'\n')
        #line = json.dumps(M0,ensure_ascii=False)
        #f.write(line.encode('utf-8')+'\n')
        for m in M0:
            for k,v in m.items():
                #line = json.dumps(m)
                line = json.dumps((k,v),ensure_ascii=False)
                f.write(line.encode('utf-8')+'\n')
            f.write("\n")


if __name__ == '__main__':

    print 'Hi'

    location1 = 'data/issue.sim0.txt'
    location2 = 'data/issue.res0.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim1.txt'
    location2 = 'data/issue.res1.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim2.txt'
    location2 = 'data/issue.res2.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim3.txt'
    location2 = 'data/issue.res3.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim4.txt'
    location2 = 'data/issue.res4.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim5.txt'
    location2 = 'data/issue.res5.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim6.txt'
    location2 = 'data/issue.res6.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim7.txt'
    location2 = 'data/issue.res7.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim8.txt'
    location2 = 'data/issue.res8.txt'
    getCount(location1,location2)
    location1 = 'data/issue.sim9.txt'
    location2 = 'data/issue.res9.txt'
    getCount(location1,location2)

